/**
 * 表单验证
 * 
 * @param {Object} rules 验证字段的规则
 * @param {Object} messages 验证字段的提示信息
 * 
 */

import basic from '../../../xc/model/basic.js';
var actmodel = new basic();


actmodel.offset = function (that, reset, para, xcsorts) { 
  if(!xcsorts)
  {
    xcsorts=[["id", "desc"], ["modifytime", "desc"]] ;
  }
 
  var acturlpara = {};   
  acturlpara["url"] = "entry/wxapp/integralorder_bill"
  if (para) {
    para["op"] = "offset"
    para["plugin"] = "integral"
  }
  else {
    this.wherepara["op"] = "offset"
    this.wherepara["plugin"] = "integral"
  }
  return actmodel.pagelist(acturlpara, that, reset, para, xcsorts);
}
//付款以前下的单
actmodel.payoldorder=function (id) {
  var app = getApp();
  var data = { "op": "payoldorder", id: id,"plugin":'integral'}
  return new Promise((resolve, reject) => {
    app.util.request({
      'url': 'entry/wxapp/integralorder_bill',
      'data': data,
      'method': "post",
      success: function (res) {
      var reslutdata= res.data.data;
         if(reslutdata["status"]==1) {
          var template_ids = reslutdata["obj"]["template_ids"]
          var  pay_params =reslutdata["obj"]["pay_params"];        
          wx.requestPayment({
            timeStamp: pay_params["timeStamp"],
            nonceStr:pay_params["nonceStr"],
            package:pay_params["package"],
            signType:pay_params['signType'],
            paySign: pay_params["paySign"],
            success (res) {   
              wx.showToast({
                title: '支付成功',
              })
              if (template_ids&&template_ids.length>0) {
                wx.requestSubscribeMessage({
                  tmplIds: template_ids,
                  success(res) { }
                })
              }

              actmodel.queryorder(id).then(function (queryreslut) {
                if (queryreslut["status"] == 1) {
                  resolve({op:"paysuccess"})
                }
                else
                {
                  resolve({op:"payfail"})
                }                
              })

         
             },
            fail (res) { 
              if(res["errMsg"]=="requestPayment:fail cancel") {
                wx.showModal({
                  title: '提示',
                  content: '你已取消支付',  
                  showCancel:false              
                })
                
              }           
              resolve({op:"payfail"})
            }
          })
         }
         else
         {
           //服务器错误操作
          resolve({op:"sendoder",data:reslutdata})
         } 
      },
      fail: function (res) {

        reject(((res.data && res.data.message) ? res.data.message : res.errMsg));
      },
      complete: function () {
      }
    });
  });
}
actmodel.queryorder=function (id) {
  var app = getApp();
  var data = { "op": "queryorder", id: id,"plugin":'integral'}
  return new Promise((resolve, reject) => {
    app.util.request({
      'url': 'entry/wxapp/integralorder_bill',
      'data': data,
      'method': "post",
      success: function (res) {
        resolve(res.data.data);
      }
    })
})
}

actmodel.closedorder=function(id){
  var app = getApp();
  var data = { "op": "closedorder", id: id,"plugin":'integral'}
  return new Promise((resolve, reject) => {
    app.util.request({
      'url': 'entry/wxapp/integralorder_bill',
      'data': data,
      'method': "post",
      success: function (res) {        
        resolve(res.data.data);
      }
    })
})
}
actmodel.getcutlist = function (strwhere) {
  var app = getApp();
  var objwhere = {
    "op": "getcutlist",
    strwhere: JSON.stringify(strwhere)
  };
  return new Promise((resolve, reject) => {
    app.util.request({
      'url': "entry/wxapp/order_bill",
      'data': objwhere,
      'method': "post",
      success: function (res) {
        resolve(res.data.data);
      },
      fail: function (res) {

      },
      complete: function () {}
    });
  });
}

actmodel.createorder = function (model) {
  var app = getApp();
  var data = { "op": "createorderv2", model: model ,plugin:'integral'}
  return new Promise((resolve, reject) => {
    app.util.request({
      'url': 'entry/wxapp/integralorder_bill',
      'data': data,
      'method': "get",
      success: function (res) {
        if (res.data.errno == 0) {
          resolve(res.data.data);
        }
        reject(res.data.message);
      },
      fail: function (res) {
        
        reject(((res.data && res.data.message) ? res.data.message : res.errMsg));
      },
      complete: function () {
      }
    });
  });
}

actmodel.refundapply = function (model) {
  var app = getApp();
  var data = { "op": "refundapply", model: JSON.stringify(model) }
  return new Promise((resolve, reject) => {
    app.util.request({
      'url': 'entry/wxapp/order_bill',
      'data': data,
      'method': "get",
      success: function (res) {
        if (res.data.errno == 0) {

          resolve(res.data.data);

        }
        reject(res.data.message);
      },
      fail: function (res) {

        reject(((res.data && res.data.message) ? res.data.message : res.errMsg));
      },
      complete: function () {
      }
    });
  });
}



actmodel.finishbill=function(id){
  var app = getApp();
  var data = { "op": "finishbill", id: id,'plugin':'integral'}
  return new Promise((resolve, reject) => {
    app.util.request({
      'url': 'entry/wxapp/integralorder_bill',
      'data': data,
      'method': "post",
      success: function (res) {        
        resolve(res.data.data);
      }
    })
})
}


actmodel.getmodel=function($id){
  var app = getApp();
  var data = { "op": "getmodel", id: $id }
  return new Promise((resolve, reject) => {
    app.util.request({
      'url': 'entry/wxapp/order_bill',
      'data': data,
      'method': "get",
      success: function (res) {
        if (res.data.errno == 0) {
          resolve(res.data.data);
        }
        reject(res.data.message);
      },
      fail: function (res) {

        reject(((res.data && res.data.message) ? res.data.message : res.errMsg));
      },
      complete: function () {
      }
    });
  });

}
actmodel.comfirm=function($id){
  var app = getApp();
  var data = { "op": "comfirm", id: $id }
  return new Promise((resolve, reject) => {
    app.util.request({
      'url': 'entry/wxapp/order_bill',
      'data': data,
      'method': "get",
      success: function (res) {
        if (res.data.errno == 0) {

          resolve(res.data.data);

        }
        reject(res.data.message);
      },
      fail: function (res) {

        reject(((res.data && res.data.message) ? res.data.message : res.errMsg));
      },
      complete: function () {
      }
    });
  });

}
actmodel.detailrefund=function($id){
  var app = getApp();
  var data = { "op": "detailrefund", id: $id }
  return new Promise((resolve, reject) => {
    app.util.request({
      'url': 'entry/wxapp/order_bill',
      'data': data,
      'method': "get",
      success: function (res) {
        if (res.data.errno == 0) {

          resolve(res.data.data);

        }
        reject(res.data.message);
      },
      fail: function (res) {

        reject(((res.data && res.data.message) ? res.data.message : res.errMsg));
      },
      complete: function () {
      }
    });
  });

}
actmodel.tool=function ($id,childop) {
  var app = getApp();
  var data = { "op": "tool", id: $id,childop:childop}
  return new Promise((resolve, reject) => {
    app.util.request({
      'url': 'entry/wxapp/order_bill',
      'data': data,
      'method': "get",
      success: function (res) {
        if (res.data.errno == 0) {

          resolve(res.data.data);

        }
        reject(res.data.message);
      },
      fail: function (res) {
        reject(((res.data && res.data.message) ? res.data.message : res.errMsg));
      },
      complete: function () {
      }
    });
  });
}
actmodel.evaluate = function (model) {
  var app = getApp();
  var data = { "op": "evaluate", model: JSON.stringify(model) }
  return new Promise((resolve, reject) => {
    app.util.request({
      'url': 'entry/wxapp/order_bill',
      'data': data,
      'method': "post",
      success: function (res) {
        if (res.data.errno == 0) {

          resolve(res.data.data);

        }
        reject(res.data.message);
      },
      fail: function (res) {

        reject(((res.data && res.data.message) ? res.data.message : res.errMsg));
      },
      complete: function () {
      }
    });
  });
}
actmodel.getexpress = function (model,addr) {
  var app = getApp();

 
  var exppro=[];
  var expprokey=['proid','weight','activityid','num','realprice','activityprice','keyval',"pro_ident"]
  model.forEach(item=>{
    var temmodel={}
    expprokey.forEach(keyitem=>{
      temmodel[keyitem]=item[keyitem]
    })    
    exppro.push(temmodel)
  })

var addrkey=['province','city','county']
var simpleaddr={};
  addrkey.forEach(keyitem=>{
    simpleaddr[keyitem]=addr[keyitem]
  })   

  var data = { "op": "getexpress", model: JSON.stringify(exppro) ,addr: JSON.stringify(simpleaddr)}
  return new Promise((resolve, reject) => {
    app.util.request({
      'url': 'entry/wxapp/order_bill',
      'data': data,
      'method': "post",
      success: function (res) {
        if (res.data.errno == 0) {
          resolve(res.data.data);
        }
        reject(res.data.message);
      },
      fail: function (res) {

        reject(((res.data && res.data.message) ? res.data.message : res.errMsg));
      },
      complete: function () {
      }
    });
  });
}
actmodel.sendnotice_buy=function (ordernum) {

  var app = getApp();
  var data = { "op": "sendnotice_buy", ordernum: ordernum}
  return new Promise((resolve, reject) => {
    app.util.request({
      'url': 'entry/wxapp/order_bill',
      'data': data,
      'method': "post",
      success: function (res) {
        
        resolve(res.data.data);
      }
    })
})
}
module.exports = actmodel;